package hotel.switchs.oversea.online.controller;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.CreateRoomResultVo;
import hotel.switchs.oversea.api.VO.DltBasicRoomTypeVo;
import hotel.switchs.oversea.api.VO.RoomSaleRuleReturnVo;
import hotel.switchs.oversea.api.VO.request.*;
import hotel.switchs.oversea.api.entity.SwitchCreateRoomEntity;
import hotel.switchs.oversea.online.service.SwitchBasicRoomTypeService;
import hotel.switchs.oversea.online.service.SwitchCreateRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/5/16 16:12
 * 物理房型控制器
 **/
@Slf4j
@RestController
@Api(value = "SwitchBasicRoomController",tags = {"获取母物理房型信息"})
@RequestMapping("/switchBasicRoomTypeInfo")
public class SwitchBasicRoomController {
    @Autowired
    SwitchBasicRoomTypeService switchBasicRoomTypeService;
    @Autowired
    private SwitchCreateRoomService switchCreateRoomService;
    /**
     *获取母物理房型列表
     */
    @RequestMapping("/getdltbasicroomlist")
    @ApiOperation(value ="获取母物理房型列表",tags = {"必须经过oauth2授权"})
    public Result<Object> getDltBasicRoomList(@RequestParam(value = "hotelId")Integer hotelID) {
        return switchBasicRoomTypeService.getDltBasicRoomList(hotelID);
    }
    @PostMapping("/saveRoomTypeInfos")
    @ApiOperation(value = "保存物理房型接口",notes="必须经过auth授权")
    public Result<Object> saveRoomTypeInfos(@RequestBody SwitchRoomTypeRedisParamsVo switchBasicRoomTypeRedisParamsVo){
        //发起请求并保存数据
        Map<String,List<DltBasicRoomTypeVo>> dltBasicRoomTypeVosMap = switchBasicRoomTypeRedisParamsVo.getDltBasicRoomTypeVosMap();
        if(null == dltBasicRoomTypeVosMap || dltBasicRoomTypeVosMap.size()==0){
            return new Result<>("参数dltBasicRoomTypeVosMap不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchBasicRoomTypeService.saveRoomTypeInfos(dltBasicRoomTypeVosMap);
    }
    /**
     * 获取酒店房型静态信息列表
     */
    @RequestMapping("/gethotelroomstaticinfo")
    @ApiOperation(value = "获取酒店房型静态信息列表",tags = {"必须经过oauth2授权"})
    public Result<Object> gethotelroomstaticinfo(@RequestBody Map<String,Object> params){
        Map<String,Object> param=new HashMap<>();
        Integer  hotelId= (Integer) params.get("hotelId");
        if(hotelId==0){
            return new Result<>("子酒店id不能为空");
        }
        Integer  masterHotelID= (Integer) params.get("masterHotelId");
        if(masterHotelID==0){
            return new Result<>("母酒店id不能为空");
        }
        param.put("hotelId",hotelId);
        List<String> returnDataTypeList=new ArrayList<>();
        //获取物理房型信息
        returnDataTypeList.add("BasicRoomTypeSimpleEntity");
        //物理房型设施设备信息
        returnDataTypeList.add("BasicRoomTypeFacilityEntityList");
        //物理房型床型信息
        returnDataTypeList.add("BasicRoomTypeBedTypeEntity");
       /* //售卖房型网络信息
        returnDataTypeList.add("RoomTypeBroadnNetEntity");
        //售卖房型无烟信息
        returnDataTypeList.add("RoomTypeSmokeInfoEntity");
        //供应商信息
        returnDataTypeList.add("SupplierInfoEntity");
        //物理房型图片信息
        returnDataTypeList.add("BasicRoomType_HotelBasicRoomPictureEntityList");*/
        param.put("returnDataTypeList",returnDataTypeList);
        return switchBasicRoomTypeService.getHotelRoomStaticInfo(param,masterHotelID);
       /* return switchBasicRoomTypeService.getHotelRoomStaticInfoList(hotelId,masterHotelID);*/
    }
    @PostMapping("/saveBasicRoomInfos")
    @ApiOperation(value = "保存静态物理房型接口",notes="必须经过auth授权")
    public Result<Object> saveBasicRoomInfos(@RequestBody String vo){
        log.info("/////////////////////////  saveBasicRoomInfos  saveBasicRoomInfos  :"+ JSON.toJSONString(vo));
        //SwitchBasicRoomDataRedisParamsVo 嵌套实体对象太多 跑批过程出现400错误码 暂时改为String 接收参数
        SwitchBasicRoomDataRedisParamsVo roomStaticInfoListMap = JSON.parseObject(vo, SwitchBasicRoomDataRedisParamsVo.class);
        //发起请求并保存数据
        if(null == roomStaticInfoListMap ||null == roomStaticInfoListMap.getRoomStaticInfoListMap() || roomStaticInfoListMap.getRoomStaticInfoListMap().size()==0){
            return new Result<>("参数roomStaticInfoListMap不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchBasicRoomTypeService.saveBasicRoomData(roomStaticInfoListMap.getRoomStaticInfoListMap());
    }
    /**
     * 新增子物理房型
     */
    @PostMapping("/createBasicRoom")
    @ApiOperation(value = "新建子物理房型",tags = {"必须经过oauth2授权"})
    public Result<Object> createBasicRoom(@RequestBody CreateBasicRoomVo createBasicRoomVo){
        return switchBasicRoomTypeService.createBasicRoom(createBasicRoomVo);
    }

    /**
     * 更新子物理房型
     */
    @PostMapping("/updateBasicRoom")
    @ApiOperation(value = "更新子物理房型",tags = {"必须经过oauth2授权"})
    public Result<Object> updateBasicRoom(@RequestBody UpdateBasicRoomVo updateBasicRoomVo){
       return switchBasicRoomTypeService.updateBasicRoom(updateBasicRoomVo);
    }
    /**
     * 设置售卖房型上下线
     */
    @PostMapping("/setRoomOnlineOffline")
    @ApiOperation(value = "设置售卖房型上下线",tags = {"必须经过oauth2授权"})
    public Result<Object> setRoomOnlineOffline(@RequestBody SetRoomOnlineOfflineVo setRoomOnlineOfflineVo){
        return switchBasicRoomTypeService.setRoomOnlineOffline(setRoomOnlineOfflineVo);
    }
    /**
     * 更改售卖房型渠道名称
     */
    @PostMapping("/updateRoomSaleName")
    @ApiOperation(value = "更改售卖房型渠道名称",tags = {"必须经过oauth2授权"})
    public Result<Object> updateRoomSaleName(@RequestBody UpdateRoomSaleNameVo updateRoomSaleNameVo){
        Map<String,Object> map=new HashMap<>();
        int hotelId=updateRoomSaleNameVo.getHotelId();
        if(hotelId==0){
            return new Result<>("子酒店不能为空");
        }
        map.put("hotelId",hotelId);
        int roomTypeId=updateRoomSaleNameVo.getRoomTypeId();
        if(roomTypeId==0){
            return new Result<>("售卖房型id不能为空");
        }
        map.put("roomTypeId",roomTypeId);
        map.put("ctripName", StringUtils.isEmpty(updateRoomSaleNameVo.getCtripName())?"":updateRoomSaleNameVo.getCtripName());
        map.put("senderName", StringUtils.isEmpty(updateRoomSaleNameVo.getSenderName())?"":updateRoomSaleNameVo.getSenderName());
        map.put("qunarRatePlanName", StringUtils.isEmpty(updateRoomSaleNameVo.getQunarRatePlanName())?"":updateRoomSaleNameVo.getQunarRatePlanName());
        map.put("channelARatePlanName", StringUtils.isEmpty(updateRoomSaleNameVo.getChannelARatePlanName())?"":updateRoomSaleNameVo.getChannelARatePlanName());
        map.put("b2bRatePlanName", StringUtils.isEmpty(updateRoomSaleNameVo.getB2bRatePlanName())?"":updateRoomSaleNameVo.getB2bRatePlanName());
        return switchBasicRoomTypeService.updateRoomSaleName(map);
    }

    /**
     * 设置房型库存房态共享
     */
    @PostMapping("/setRoomShare")
    @ApiOperation(value = "设置房型库存房态共享",tags = {"必须经过oauth2授权"})
    public Result<Object> setRoomShare(@RequestBody SetRoomShareVo setRoomShareVo){
        return switchBasicRoomTypeService.setRoomShare(setRoomShareVo);
    }
    /**
     * 创建售卖房型
     */
    @PostMapping("/createRoom")
    @ApiOperation(value = "创建售卖房型",tags = {"必须经过oauth2授权"})
    public Result<CreateRoomResultVo> createRoom(@RequestBody CreateRoomVo createRoomVo){
        return switchBasicRoomTypeService.createRoom(createRoomVo);
    }
    /**
     * 更新售卖房型
     */
    @PostMapping("/updateRateCode")
    @ApiOperation(value = "更新售卖房型RateCode",tags = {"必须经过oauth2授权"})
    public Result<Object> updateRateCode(@RequestBody UpdateRateCodeNewVo updateRateCodeNewVo){
        if(ObjectUtils.isEmpty(updateRateCodeNewVo.getMtHotelId())||updateRateCodeNewVo.getMtHotelId()<1){
            return new Result<>("美团的酒店id为空");
        }
        if(ObjectUtils.isEmpty(updateRateCodeNewVo.getMtGoodId())||updateRateCodeNewVo.getMtGoodId()<0){
            return new Result<>("美团的产品id为空");
        }
        SwitchCreateRoomEntity switchCreateRoomEntity=switchCreateRoomService.selectMtHotelAndMtGoodId(updateRateCodeNewVo.getMtHotelId(),updateRateCodeNewVo.getMtGoodId());
        UpdateRateCodeVo updateRateCodeVo=new UpdateRateCodeVo();
        BeanUtils.copyProperties(updateRateCodeVo,updateRateCodeNewVo);
        updateRateCodeVo.setRoomName(switchCreateRoomEntity.getRoomName());
        updateRateCodeVo.setHotelId(switchCreateRoomEntity.getHotelId());
        updateRateCodeVo.setRoomTypeId(switchCreateRoomEntity.getRoomId());
        return switchBasicRoomTypeService.updateRateCode(updateRateCodeVo);
    }

    /**
     * 更新房型售卖规则
     */
    @PostMapping("/setRoomSaleRule")
    @ApiOperation(value = "更新房型售卖规则",tags = {"必须经过oauth2授权"})
    public Result<Object> setRoomSaleRule(@RequestBody SetRoomSaleRuleVo setRoomSaleRuleVo ) {
        return switchBasicRoomTypeService.setRoomSaleRule(setRoomSaleRuleVo);
    }
    /**
     * 获取房型售卖规则
     */
    @PostMapping("/getRoomSaleRule")
    @ApiOperation(value = "获取房型售卖规则",tags = {"必须经过oauth2授权"})
    public Result<List<RoomSaleRuleReturnVo>>  getRoomSaleRule(@RequestBody GetRoomSaleRuleVo getRoomSaleRuleVo){
        Map<String, Object> map = new HashMap<>();
        int hotelId = getRoomSaleRuleVo.getHotelId();
        if (hotelId == 0) {
            return new Result<>("子酒店不能为空");
        }
        map.put("hotelId", hotelId);
        int[] roomIds=getRoomSaleRuleVo.getRoomIds();
        if(roomIds.length<1){
            return new Result<>("售卖房型ID不能为空");
        }
        map.put("roomIds",roomIds);
        String[] channels=getRoomSaleRuleVo.getChannels();
        if(channels.length<1){
            return new Result<>("房型售卖渠道不能为空");
        }
        map.put("channels",channels);
        map.put("returnAllRooms",getRoomSaleRuleVo.isReturnAllRooms());
        return switchBasicRoomTypeService.getRoomSaleRule(map);
    }
    /**
     *获取母物理房型列表
     */
    @PostMapping("/getRoomListByHotelId")
    @ApiOperation(value ="获取母物理房型列表",tags = {"必须经过oauth2授权"})
    public Result<Object> getRoomListByHotelId(@RequestBody Map<String,Integer> params) {
        if(!params.containsKey("hotelId")){
            return new Result<>();
        }
        int hotelId=params.get("hotelId");
        return switchBasicRoomTypeService.getRoomListByHotelId(hotelId);
    }
}
